﻿using System.Collections.Generic;
using System.IO;
using Android.Content;
using Android.Graphics;
using Android.Runtime;
using Android.Views;
using Android.Widget;
using Java.Lang;

namespace SCPAK2
{
    public class FileListAdapter : BaseAdapter
    {
        public List<FileItem> list = new List<FileItem>();
        private Context context;
        public FileListAdapter(Context _)
        {
            context = _;
        }
        public override Object GetItem(int position)
        {
            return null;
        }
        public void Add(List<FileItem> items) {
            list.AddRange(items);
        }
        public void Add(FileItem data)
        {
            list.Add(data);
        }
        public void Clear()
        {
            list.Clear();
        }

        public FileItem GetItem2(int position)
        {
            return list[position];
        }

        public override long GetItemId(int position)
        {
            return position;
        }

        public override View GetView(int position, View view, ViewGroup parent)
        {
            var inflater = context.GetSystemService(Context.LayoutInflaterService).JavaCast<LayoutInflater>();
            view = inflater.Inflate(Resource.Layout.fileList_item, parent, false);
            if (list.Count < 1)
            {
                return view;
            }

            FileItem fileItem = list[position];
            if (fileItem.IsDir)
            {
                view.FindViewById<ImageView>(Resource.Id.fileIcon).SetImageResource(Resource.Drawable.file_dir);
            }
            else
            {
                ImageView imageView = view.FindViewById<ImageView>(Resource.Id.fileIcon);
                switch (fileItem.Extension)
                {
                    case ".bmp":
                    case ".jpg":
                    case ".jpeg":
                    case ".png":
                        {
                            imageView.SetImageResource(Resource.Drawable.filetype_png);
                            break;
                        }
                    case ".dae":
                        {
                            imageView.SetImageResource(Resource.Drawable.filetype_dae);
                            break;

                        }
                    case ".dll":
                        {
                            imageView.SetImageResource(Resource.Drawable.filetype_dll);
                            break;
                        }
                    case ".apk":
                    case ".scmod":
                    case ".zip":
                        {
                            imageView.SetImageResource(Resource.Drawable.filetype_zip);
                            break;
                        }
                    case ".pak":
                        {
                            imageView.SetImageResource(Resource.Drawable.filetype_pak);
                            break;
                        }
                    case ".cr":
                    case ".clo":
                    case ".xdb":
                    case ".xml":
                        {
                            imageView.SetImageResource(Resource.Drawable.filetype_xml);
                            break;
                        }
                    case ".mp3":
                    case ".ogg":
                    case ".wav":
                        {
                            imageView.SetImageResource(Resource.Drawable.filetype_mp3);
                            break;
                        }
                    case ".txt":
                    case ".csv":
                        {
                            imageView.SetImageResource(Resource.Drawable.filetype_text);
                            break;
                        }
                    default:
                        {
                            imageView.SetImageResource(Resource.Drawable.file_file);
                            break;
                        }
                }
            }
            view.FindViewById<TextView>(Resource.Id.itemName).Text = fileItem.FileName;
            return view;
        }
        public void iteamTouch(object obj, View.TouchEventArgs args)
        {
            LinearLayout ll = (LinearLayout)obj;
            ll.SetBackgroundColor(Color.Bisque);
        }
        //Fill in cound here, currently 0
        public override int Count
        {
            get
            {
                return list.Count;
            }
        }

    }
}